var Pay = {
    time: 1800,
    interval: null,
    intervalMsg: null,
    changeNo: null,
    showFailMsg: false,
    paid: false,
    init: function () {
        window['cr'] = {
            receiveMessage: function (e) {
                var msg = JSON.parse(e);
                if (msg.event === 406) {
                    // V7:1
                    // V8:2
                    // 3U:3
                    if (Pay.paid) {
                        Pay.closeWindow();
                        return;
                    }
                    var v = localStorage.getItem('elVersion');
                    if (v == 1) {
                        Pay.showVSevenCloseMsg();
                    }
                    if (v == 2) {
                        Pay.showVEightCloseMsg();
                    }
                }
            }
        };
        $('#payContent').hide();
        $('#successContent').hide();
        $('.VSeven').hide();
        $('.VEight').hide();
        var p = Pay.getLocationSearchObj();
        Pay.buy(p);
        zz_send(JSON.stringify({event: 100}));
        zz_send(JSON.stringify({event: 405, title: '购买验机分'}));
        // 兼容V8
        window._pcReady = function () {
            zz_send(JSON.stringify({event: 100}));
            zz_send(JSON.stringify({event: 405, title: '购买验机分'}));
        };
    },
    buy: function (info) {
        ChargingAjax.buy({
            frequency: info.level, //充值分数
            aiDou: info.aidou
        }, function (data) {
            try {
                console.log(data);
                $('#payContent').show();
                $('#payPrice').text(data.data.needPay);
                Pay.changeNo = data.data.changeNo;
                Pay.time = data.data.overTime / 1000;
                Pay.countdown();
                // Pay.qrcode(data.data.wxCode, data.data.aliCode);
                Pay.qrcodeUnionPay(data.data.unionPayCode);
                setInterval(function () {
                    Pay.getPayStatus();
                }, 1000);
                $('#getPayStatus').click(function () {
                    Pay.showFailMsg = true;
                    Pay.getPayStatus();
                });
            } catch (e) {
                $('#payContent').hide();
                $('#successContent').show();
                $('#back').click(function () {
                    window.location.href = 'chargingMessage.html';
                });
                $('#paySuccessPrice').text('已成功支付0元');
                Pay.paid = true;
            }
        });
    },
    qrcode: function (wx, zfb) {
        var qrcodeWX = new QRCode(document.getElementById('qrcodeWX'), {
            width: 160,
            height: 160,
            useSVG: false
        });
        qrcodeWX.makeCode(wx);
        var qrcodeZFB = new QRCode(document.getElementById('qrcodeZFB'), {
            width: 160,
            height: 160,
            useSVG: false
        });
        qrcodeZFB.makeCode(zfb);
        $('#qrcodeWX').attr('title', '');
        $('#qrcodeZFB').attr('title', '');
    },
    qrcodeUnionPay: function (url) {
        var qrcodeZFB = new QRCode(document.getElementById('qrcodeZFB'), {
            width: 160,
            height: 160,
            useSVG: false
        });
        qrcodeZFB.makeCode(url);
        $('#qrcodeZFB').attr('title', '');
    },
    countdown: function () {
        var minute = Math.floor(Pay.time / 60);
        var second = Pay.time % 60;
        if (minute.toString().length == 1) {
            minute = '0' + minute.toString();
        }
        if (second.toString().length == 1) {
            second = '0' + second.toString();
        }
        $('#time').text(minute + '分' + second + '秒');
        Pay.interval = setInterval(function () {
            Pay.time -= 1;
            var minute = Math.floor(Pay.time / 60);
            var second = Pay.time % 60;
            if (minute.toString().length == 1) {
                minute = '0' + minute.toString();
            }
            if (second.toString().length == 1) {
                second = '0' + second.toString();
            }
            $('#time').text(minute + '分' + second + '秒');
            if (Pay.time <= 0) {
                Pay.clearInterval();
                Pay.closeWindow();
            }
        }, 1000);
    },
    clearInterval: function () {
        Pay.time = 1800;
        clearInterval(Pay.interval);
    },
    getLocationSearchObj: function () {
        var obj = {};
        var arr = window.location.href.split('?').pop().split('#').shift().split('&');
        for (var i = arr.length - 1; i >= 0; i--) {
            if (arr[i].match('aidou=')) {
                obj.aidou = arr[i].split('aidou=')[1];
            }
            if (arr[i].match('level=')) {
                obj.level = arr[i].split('level=')[1];
            }
            if (arr[i].match('json=')) {
                obj.json = arr[i].split('json=')[1];
            }
        }
        return obj;
    },
    getPayStatus: function () {
        if (Pay.changeNo == null) {
            return;
        }
        ChargingAjax.getPayStatus(function (data) {
            $('#failMessage').css('display', 'none');
            if (data.code == 500 && Pay.showFailMsg) {
                $('#failMessage').css('display', 'block');
            }
            if (data.code == 0) {
                $('#payContent').hide();
                $('#successContent').show();
                $('#paySuccessPrice').text('已成功支付' + data.data + '元');
                Pay.paid = true;
                $('#back').click(function () {
                    window.location.href = 'chargingMessage.html';
                });
            }
        });
    },
    showVSevenCloseMsg: function () {
        $('.VSeven').show();
        Pay.setIntervalMsg();
        $('.closeAlert').click(function () {
            Pay.closeWindow();
        });
        $('.hideAlert').click(function () {
            Pay.hideAlert();
        });
        $('.closeBtn').click(function () {
            Pay.hideAlert();
        });
    },
    showVEightCloseMsg: function () {
        $('.VEight').show();
        Pay.setIntervalMsg();
        $('.closeAlert').click(function () {
            Pay.closeWindow();
        });
        $('.hideAlert').click(function () {
            Pay.hideAlert();
        });
        $('.closeBtn').click(function () {
            Pay.hideAlert();
        });
    },
    setIntervalMsg: function () {
        var minute = Math.floor(Pay.time / 60);
        var second = Pay.time % 60;
        if (minute.toString().length == 1) {
            minute = '0' + minute.toString();
        }
        if (second.toString().length == 1) {
            second = '0' + second.toString();
        }
        $('.alertText').text('您的订单在' + minute + '分' + second + '秒' + '内未支付将自动取消，请尽快完成支付。');
        Pay.intervalMsg = setInterval(function () {
            if (Pay.time <= 0) {
                clearInterval(Pay.intervalMsg);
            }
            minute = Math.floor(Pay.time / 60);
            second = Pay.time % 60;
            if (minute.toString().length == 1) {
                minute = '0' + minute.toString();
            }
            if (second.toString().length == 1) {
                second = '0' + second.toString();
            }
            $('.alertText').text('您的订单在' + minute + '分' + second + '秒' + '内未支付将自动取消，请尽快完成支付。');
        }, 250);
    },
    hideAlert: function () {
        clearInterval(Pay.intervalMsg);
        $('.VEight').hide();
        $('.VSeven').hide();
    },
    closeWindow: function () {
        zz_send(JSON.stringify({event: 404}));
    }
};
